import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Test {

    public static void main(String[] args) {

        Utils utils = new Utils();
        int[] initSize = utils.getInitSize();

        JFrame jFrame = new JFrame();
        Test test = new Test();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // 关闭事件
        String iconPath = Test.class.getClassLoader().getResource("img/icon.png").getPath();
        jFrame.setIconImage(new ImageIcon(iconPath).getImage());    // icon图标
        jFrame.setSize(initSize[0], initSize[1]);
        jFrame.setLayout(null); // 设置为null，代表自由布局
        jFrame.setTitle("TTT"); // 设置标题名称
        jFrame.setLocationRelativeTo(null); // 设置剧中显示
        jFrame.setUndecorated(true);    // 去掉默认的窗口【标题栏】装饰器
        jFrame.setBackground(new Color(75, 71, 71, 200));
        jFrame.setMinimumSize(new Dimension(initSize[0], initSize[1]));  // 设置window最小值

        //  设置按钮
        JButton loginBtn = new JButton("login");
        loginBtn.setBounds(jFrame.getWidth() / 10, jFrame.getHeight() / 10, 60, 36);
        Color color = new Color(53, 134, 229, 253);
        loginBtn.setBackground(color);
        loginBtn.setFocusPainted(false);    // 去掉文字的虚线框
        // loginBtn.setContentAreaFilled(false);    // 透明按钮
        loginBtn.setBorderPainted(false);
        Color color2 = new Color(211, 229, 53, 253);
        loginBtn.setForeground(color2); // 前景色
        // loginBtn.setBorder(BorderFactory.createLoweredBevelBorder());    // 设置边框样式
        // 关闭进程和窗口事件
        loginBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int doNothingOnClose = JFrame.DO_NOTHING_ON_CLOSE;
                System.exit(doNothingOnClose);
            }
        });

        /*
        * 放大、缩小、关闭 Button
        * */
        JPanel compress = new JPanel();
        JPanel expand = new JPanel();
        JPanel exit = new JPanel();

        int[] frameSize = utils.getFrameCurrentSize(jFrame);


        compress.setBounds(frameSize[0]-20,5,15,15);
        expand.setBounds(frameSize[0]-40,5,15,15);
        exit.setBounds(frameSize[0]-60,5,15,15);

        jFrame.add(loginBtn);
        jFrame.add(compress);
        jFrame.add(expand);
        jFrame.add(exit);


        // 动态改变组件大小
        jFrame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                int resize = test.getResize(jFrame);
                loginBtn.setBounds(jFrame.getWidth() / 10, jFrame.getHeight() / 10, resize * 60, resize * 36);
            }
        });

        // 自定义窗口拖动事件 和 装口移动事件
        ReSizeEvent reSizeEvent = new ReSizeEvent(jFrame);
        jFrame.addMouseListener(reSizeEvent);
        jFrame.addMouseMotionListener(reSizeEvent);

        // 显示
        jFrame.setVisible(true);
    }

    public int getResize(JFrame jFrame) {
        double widthPro = jFrame.getWidth();
        Utils utils = new Utils();
        int widthRelease = (int) (widthPro / (double) utils.getInitSize()[0]);
        return widthRelease;
    }
}
